/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.web.controller;

import com.andersen.web.service.*;
import com.andersen.web.utils.DataTableJsonUtils;
import com.andersen.web.utils.RequestUtils;
import java.text.SimpleDateFormat;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

/**
 *
 * @author Corvus 11
 */
@Controller
public abstract class AbstractController {
    
    protected static final String PARAM_VAL_SORT_ASC = "asc";
    protected static final String PARAM_VAL_STATUS_OK = "0";
    
    protected static final String PARAM_NAME_DISPLAY_START = "iDisplayStart";
    protected static final String PARAM_NAME_DISPLAY_COUNT = "iDisplayLength";
    protected static final String PARAM_NAME_SORT_BY = "iSortCol_0";
    protected static final String PARAM_NAME_SORT_ORDER = "sSortDir_0";
    protected static final String PARAM_NAME_ECHO = "sEcho";
    
    protected static final String RESULT_ATTRIBUTE_ECHO = "sEcho";
    protected static final String RESULT_ATTRIBUTE_DATA = "aaData";
    protected static final String RESULT_ATTRIBUTE_TOTAL_RECORDS = "iTotalRecords";
    protected static final String RESULT_ATTRIBUTE_DISPLAY_RECORDS = "iTotalDisplayRecords";
    protected static final String RESULT_ATTRIBUTE_ERROR_VAL = "errorVal";
    protected static final String RESULT_ATTRIBUTE_DELETED_COMPONENT_ID = "deletedId";
    protected static final String RESULT_ATTRIBUTE_DISCONNECTED_COMPUTER = "disconnectedComputer";
    protected static final String RESULT_ATTRIBUTE_DISCONNECTED_EMPLOYEE = "disconnectedEmployee";
    protected static final String RESULT_ATTRIBUTE_DISCONNECTED_COMPONENTS = "disconnectedComponents";
    protected static final String RESULT_ATTRIBUTE_MOVED_COMPONENTS = "movedComponents";
    
    protected static final String ERROR_DATA_PROCESSING_FAILED = "request_error";
    protected static final String ERROR_STATE_DATE_BUSY = "Error_date_busy";
    protected static final String ERROR_STATE_DATE_IN_FUTURE = "Error_date_in_future";
    protected static final String ERROR_VAL_OK = "OK";
    
    protected static final long ID_NOT_DEFINED = 0l;
    protected static final long ID_INVALID = -1l;
    
    protected static final SimpleDateFormat localDateFormat = new SimpleDateFormat("dd.MM.yyyy");
    
    protected static final String VIEW_COMPONENTS = "components";
    protected static final String VIEW_DIALOG_COMPONENT_PROPERTIES = "templates/dialog_component_properties";
    protected static final String VIEW_DIALOG_COMPONENT_PROPERTIES_READONLY = "templates/dialog_component_properties_readonly";
    protected static final String VIEW_DIALOG_NEW_COMPONENT_DATA = "templates/dialog_new_component_data";
    protected static final String VIEW_DIALOG_NEW_COMPONENT_RELATION = "templates/dialog_new_component_relation";
    protected static final String VIEW_DIALOG_DELETE_COMPONENT = "templates/dialog_delete_component";
    protected static final String VIEW_DIALOG_MOVE_COMPONENT_TO_EMPLOYEE = "templates/dialog_move_to_employee";
    protected static final String VIEW_DIALOG_MOVE_COMPONENT_TO_OFFICE = "templates/dialog_move_to_office";

    
    @Autowired
    protected CustomUserDetailsService userService;
    
    @Autowired
    protected EmployeeDetailsService employeeService;

    @Autowired
    protected OfficeDetailsService officeService;

    @Autowired
    protected ComponentTypeService componentTypeService;

    @Autowired
    protected  ComponentService componentsService;
    
    @Autowired
    protected StateService stateService;
    
    @Autowired
    protected RequestUtils requestUtils;
    
    @Autowired
    protected DataTableJsonUtils dataTableJsonUtils;
    
    protected static final Logger LOG = Logger.getLogger("Controller");
    
    protected boolean hasGrantedAuthority(String roleName) {
        boolean hasRole = false;
        
        for(GrantedAuthority ga : SecurityContextHolder.getContext().getAuthentication().getAuthorities()) {
                if (ga.getAuthority().equals(roleName)) hasRole = true;
            }
        return hasRole;
    }
    
}
